package com.example.Utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 获取是否有该key
     * @param key
     * @return
     */
    public boolean HasKey(String key){
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取数据
     */
    public String getString(final String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 写入数据
     */
    public boolean setString(final String key, String value, long time) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            if (time>0)
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新数据
     */
    public boolean getAndSetString(final String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除数据
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置Hash缓存
     * @param hkey 大key
     * @param key 小key
     * @param value 值
     * @return boolean
     */
    public boolean SetHashValue(String hkey,String key,Object value){
        boolean result = false;
        try {
            redisTemplate.boundHashOps(hkey).put(key, value);
            result = true;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置Hash缓存（带时间）
     * @param hkey 大key
     * @param key 小key
     * @param value 值
     * @param expireTime 时间限制
     * @return
     */
    public boolean SetHashValue(String hkey, String key, Object value, long expireTime) {
        System.out.println("设置Hash缓存："+hkey+"-"+key);
        boolean result = false;
        try {
            // 设置哈希值
            redisTemplate.boundHashOps(hkey).put(key, value);

            // 设置过期时间
            redisTemplate.expire(hkey, expireTime, TimeUnit.SECONDS);

            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 获取具体的Hash缓存
     * @param hkey 大key
     * @param key 小key
     * @return String
     */
    public Object GetHashValue(String hkey,String key){
        System.out.println("获取具体的Hash缓存："+hkey+"-"+key);
        Object value = redisTemplate.boundHashOps(hkey).get(key);
        return value;
    }

    /**
     * 获取Hash大key下的所有value
     * @param hkey 大key
     * @return List<Object>
     */
    public List<Object> GetHashValue(String hkey){
        List<Object> values = redisTemplate.boundHashOps(hkey).values();
        return values;
    }

    /**
     * 获取某个Hash中大key下的所有key
     * @param hkey 大key
     * @return Set
     */
    public Set<Object> GetHashKeys(String hkey){
        Set<Object> s = redisTemplate.boundHashOps(hkey).keys();
        return s;
    }

    /**
     * 删除Hash中某个小key
     * @param hkey 大key
     * @param key 小key
     * @return boolean
     */
    public boolean DeleteHashKey(String hkey,String key){
        try {
            redisTemplate.boundHashOps(hkey).delete(key);
            return true;
        }
        catch (Exception e){
            return false;
        }
    }
}
